<template>
  <div>
    <div class="tree-item" @click="handleClick(node)">
      <!-- <div class="icon" v-if="hasChildren" @click="expand">点我</div> -->
      <div class="label" :class="{ selected: node.id === selected_id }">
        {{ node.name }}
      </div>
    </div>
    <div class="children" v-if="hasChildren && isExpand" style="padding-left: 20px;">
      <Node v-for="child in node.children" :key="child.id" :node="child" :selected_id="selected_id"
        @node-click="handleClick"></Node>
    </div>
  </div>

</template>
<script>
export default {
  name: 'Node',
  data() {
    return {
      isExpand: false
    }
  },
  props: {
    node: Object,
    selected_id: String | Number
  },
  computed: {
    hasChildren() {
      return this.node.children && this.node.children.length > 0
    }
  },
  methods: {
    expand() {
      this.isExpand = !this.isExpand
    },
    handleClick(node) {
      if(node.id == this.node.id) {
        this.isExpand = !this.isExpand
      }
      console.log(this.isExpand, this.node);
      this.$emit('node-click', node)
    }
  }
}
</script>
<style lang="less" scoped>
.tree-item {
  display: flex;
}

.selected {
  background-color: green;
}
</style>